if (process.env.MOCK) {
    let configArray = []
    let Mock = require('mockjs')
    Mock.setup({
        timeout: '200-600'
    })

    const files = require.context('.', true, /\.js$/)
    files.keys().forEach((key) => {
        if (key === './index.js') return
        configArray = configArray.concat(files(key).default)
    })

    configArray.forEach((item) => {
        let url = process.env.VUE_APP_BASE_API
        url += item.url
        Mock.mock(RegExp(url + '.*'), item.method, (options) => {
            console.log(options)
            let res = Mock.mock(item.response(getParam(options.url, options.body)))
            console.log(res)
            return res
        })
    })
}
/**
 *
 * @param {String} url
 * @param {(String|FormData)} body
 */
function getParam(url, body) {
    let index
    let obj = {}
    if ((index = url.indexOf('?')) > -1) {
        let queryStr = url.substring(index + 1)
        let search = new URLSearchParams(queryStr)
        obj = Object.fromEntries(search.entries())
    }
    if (body) {
        if (body instanceof FormData) {
            obj = Object.assign(obj, Object.fromEntries(body.entries()))
        } else if (typeof body == 'string' && body.startsWith('{') && body.endsWith('}')) {
            obj = Object.assign(obj, JSON.parse(body))
        } else {
            let search = new URLSearchParams(body)
            obj = Object.assign(obj, Object.fromEntries(search.entries()))
        }
    }
    console.log(obj)
    return obj
}
